//
//  addGroupViewController.m
//  QRMaster
//
//  Created by myApple on 15/10/12.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#define fontSize [UIFont systemFontOfSize:14.0]
#define viewHeight  50

#import "addGroupViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "PromptInfoView.h"
#import "groupInfoViewController.h"
#import "AppDelegate.h"
#import "RequestNetWork.h"
#import "groupModel.h"
#import "GuestModel.h"
#import "validateInputValue.h"
#import "MBProgressHUD.h"
#import "OperationDataBaseModel.h"
#import "AreaChooseController.h"


@interface addGroupViewController ()<ABPeoplePickerNavigationControllerDelegate,UITextFieldDelegate>
{
    UITextField * groupNameTextField;
    UITextField * groupManagerNameTextField;
    UITextField * groupManagerAccountTextField;
    UITextField * groupManagerCertificateTextField;
    UIButton * manBtn;
    UIButton * womanBtn;
    NSString * sexFlag;
    AppDelegate * app;
    
    UIButton * mobileBtn;
    UIButton * emailBtn;
    UIButton * mobileHeaderBtn;
    UIButton * groupManagerAccountBtn;
    UILabel * emailHeaderLabel;
    UILabel * areaCodeIdLabel;
    UILabel * areaLabel;
    
    UIView * editBackView;
}

@end

@implementation addGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    app = [UIApplication sharedApplication].delegate;
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    sexFlag = @"1";//默认为男
    [self prepareTitleView];
    [self prepareEditingView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    editBackView.hidden = NO;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     self.navigationItem.title = NSLocalizedString(@"addNewGroup", nil);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    editBackView.hidden = YES;
}
-(void)prepareTitleView
{
    editBackView = [[UIView alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH -60, 0, 40, 40)];
    [self.navigationController.navigationBar addSubview:editBackView];
    UIImageView * saveImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    saveImageView.image = [UIImage imageNamed:@"ic_save"];
    saveImageView.userInteractionEnabled = YES;
    [editBackView addSubview:saveImageView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveClick)];
    [editBackView addGestureRecognizer:editTap];
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 10;
    float y = 10;
    float width = ScreenWidth - x - 5;
    float height = viewHeight;
    
    //类型
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [self.view addSubview:typeView];
    
    UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, 20, width, 20)];
    typeLabel.text = NSLocalizedString(@"type", nil);
    typeLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:typeLabel];
    
    // mobile
    x = 90;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * mobileView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:mobileView];
    mobileView.userInteractionEnabled = YES;
    UITapGestureRecognizer * mobileTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mobileClick)];
    [mobileView addGestureRecognizer:mobileTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    mobileBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [mobileBtn addTarget:self action:@selector(mobileClick) forControlEvents:UIControlEventTouchUpInside];
    mobileBtn.selected = YES;
    [mobileView addSubview:mobileBtn];
    
    x = 130;
    y = 20;
    width = 60;
    height = 20;
    UILabel * mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mobileLabel.text =  NSLocalizedString(@"loginPromt", nil);
    mobileLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:mobileLabel];
    
    //email
    x = 190;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:emailImageView];
    emailImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailClick)];
    [emailImageView addGestureRecognizer:emailTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    emailBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [emailBtn addTarget:self action:@selector(emailClick) forControlEvents:UIControlEventTouchUpInside];
    [emailImageView addSubview:emailBtn];
    
    x = 230;
    height = 20;
    y = 20;
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    emailLabel.text =  NSLocalizedString(@"mineEmail", nil);
    emailLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:emailLabel];
    
    //组名
    x = 10;
    y = typeView.frame.origin.y + typeView.frame.size.height + 5;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [self.view addSubview:groupNameView];
    
    x = 10;
    y = 15;
    width = 90;
    height = 20;
    UILabel * groupNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameLabel.text = NSLocalizedString(@"groupName", nil);
    groupNameLabel.font = fontSize;
    [groupNameView addSubview:groupNameLabel];
    
    x = 100;
    y = 10;
    width = groupNameView.frame.size.width - x ;
    height = 30;
    groupNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameTextField.font = [UIFont systemFontOfSize:14.0];
    groupNameTextField.borderStyle = UITextBorderStyleNone;
    [groupNameView addSubview:groupNameTextField];
    
    //组管理员账号
    x = 10;
    y = 5 + groupNameView.frame.size.height + groupNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerAccountView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountView.backgroundColor = RGBA(248, 248, 248, 1);
    [self.view addSubview:groupManagerAccountView];
    
    x = 100;
    y = 10;
    width = groupManagerAccountView.frame.size.width - x - 50 ;
    height = 30;
    groupManagerAccountTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerAccountTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerAccountTextField.borderStyle = UITextBorderStyleNone;
    groupManagerAccountTextField.placeholder = NSLocalizedString(@"telePhoneNum", nil);
    groupManagerAccountTextField.keyboardType = UIKeyboardTypeNumberPad;
    [groupManagerAccountView addSubview:groupManagerAccountTextField];
    
    //前面点击的区号按钮
    x = 10;
    y = 5;
    width = 40;
    mobileHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mobileHeaderBtn.frame = CGRectMake(x, y, width, height);
    [mobileHeaderBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [groupManagerAccountView addSubview:mobileHeaderBtn];
    
    areaCodeIdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 15)];
    areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
    areaCodeIdLabel.font = [UIFont systemFontOfSize:10.0];
    [mobileHeaderBtn addSubview:areaCodeIdLabel];
    
    areaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 60, 15)];
    areaLabel.font = [UIFont systemFontOfSize:10.0];
    areaLabel.text = [QRSingleton sharedQRSingleton].currentAreaString;
    [mobileHeaderBtn addSubview:areaLabel];
    
    emailHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(mobileHeaderBtn.frame.origin.x, mobileHeaderBtn.frame.origin.y + 5, mobileHeaderBtn.frame.size.width, mobileHeaderBtn.frame.size.height)];
    emailHeaderLabel.text = NSLocalizedString(@"mineEmail", nil);
    emailHeaderLabel.hidden = YES;
    emailHeaderLabel.font = [UIFont systemFontOfSize:14.0];
    [groupManagerAccountView addSubview:emailHeaderLabel];
    
    x = groupManagerAccountView.frame.size.width - 50;
    y = 0;
    width = 50;
    height = 50;
    groupManagerAccountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    groupManagerAccountBtn.frame = CGRectMake(x, y, width, height);
    [groupManagerAccountBtn setBackgroundColor:RGBA(135, 199, 66, 1)];
    [groupManagerAccountBtn setBackgroundImage:nil forState:0];
    [groupManagerAccountBtn addTarget:self action:@selector(addTelePhone) forControlEvents:UIControlEventTouchUpInside];
    [groupManagerAccountView addSubview:groupManagerAccountBtn];
    
    UIImageView * adderssImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10, 30, 30)];
    adderssImgView.image = [UIImage imageNamed:@"ic_txl"];
    [groupManagerAccountBtn addSubview:adderssImgView];
    
    //组管理员姓名
    x = 10;
    y = 5 + groupManagerAccountView.frame.size.height + groupManagerAccountView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameView.backgroundColor = RGBA(248, 248, 248, 1);;
    [self.view addSubview:groupManagerNameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * groupManagerNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameLabel.text = NSLocalizedString(@"userName", nil);
    groupManagerNameLabel.font = fontSize;
    [groupManagerNameView addSubview:groupManagerNameLabel];
    
    x = 100;
    y = 10;
    width = groupManagerNameView.frame.size.width - x ;
    height = 30;
    groupManagerNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerNameTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerNameTextField.borderStyle = UITextBorderStyleNone;
    [groupManagerNameView addSubview:groupManagerNameTextField];
    
    //身份证
    x = 10;
    y = 5 + groupManagerNameView.frame.size.height + groupManagerNameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * groupManagerCertificateView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateView.backgroundColor = RGBA(248, 248, 248, 1);;
    [self.view addSubview:groupManagerCertificateView];
    
    x = 10;
    y = 15;
    width = 80;
    height = 20;
    UILabel * groupManagerCertificateLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateLabel.text = NSLocalizedString(@"certificateNum", nil);
    groupManagerCertificateLabel.font = fontSize;
    [groupManagerCertificateView addSubview:groupManagerCertificateLabel];
    
    x = 100;
    y = 10;
    width = groupManagerCertificateView.frame.size.width - x ;
    height = 30;
    groupManagerCertificateTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupManagerCertificateTextField.font = [UIFont systemFontOfSize:14.0];
    groupManagerCertificateTextField.borderStyle = UITextBorderStyleNone;
    [groupManagerCertificateView addSubview:groupManagerCertificateTextField];
    
    //性别
    x = 10;
    y = 5 + groupManagerCertificateView.frame.size.height + groupManagerCertificateView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = viewHeight;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);;
    [self.view addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [sexView addSubview:editSexLabel];
    
    x = 80;
    y = 0;
    width = 40;
    height = 50;
    UIImageView * manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:manBtn];
    
    x = 130;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text = NSLocalizedString(@"man", nil);
    manLabel.font = fontSize;
    [sexView addSubview:manLabel];
    
    manBtn.selected = YES;
    
    x = 180;
    y = 0;
    width = 40;
    height = viewHeight;
    UIImageView * womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text = NSLocalizedString(@"woman", nil);
    womanLabel.font = fontSize;
    [sexView addSubview:womanLabel];
    
    UITapGestureRecognizer * guestTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignClick)];
    [self.view addGestureRecognizer:guestTap];
}

#pragma mark ----------点击手机号
-(void)mobileClick
{
    [groupManagerAccountTextField resignFirstResponder];
    mobileBtn.selected = !mobileBtn.selected;
    if (mobileBtn.selected) {
        emailBtn.selected = NO;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        groupManagerAccountTextField.keyboardType = UIKeyboardTypeNumberPad;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        groupManagerAccountBtn.hidden = NO;
    }
    else
    {
        emailBtn.selected = YES;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        groupManagerAccountTextField.keyboardType = UIKeyboardTypeDefault;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        groupManagerAccountBtn.hidden = YES;
    }
}


#pragma mark ----------点击邮箱
-(void)emailClick
{
    [groupManagerAccountTextField resignFirstResponder];
    emailBtn.selected = !emailBtn.selected;
    if (emailBtn.selected) {
        mobileBtn.selected = NO;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        groupManagerAccountTextField.keyboardType = UIKeyboardTypeDefault;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        groupManagerAccountBtn.hidden = YES;
    }
    else
    {
        mobileBtn.selected = YES;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        groupManagerAccountTextField.keyboardType = UIKeyboardTypeNumberPad;
        groupManagerAccountTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        groupManagerAccountBtn.hidden = NO;
    }
}


#pragma mark ---------- 改变地区或者国家
-(void)chooseArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeString];
        areaLabel.text = areaString;
    };
}


-(void)resignClick
{
    [self hiddenKeyboard];
}

#pragma mark -- 从通讯录添加职员
-(void)addTelePhone
{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    groupManagerNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        groupManagerAccountTextField.text = [phones objectAtIndex:0];
        groupManagerAccountTextField.text = [groupManagerAccountTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    groupManagerNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        groupManagerAccountTextField.text = [phones objectAtIndex:0];
        groupManagerAccountTextField.text = [groupManagerAccountTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}

-(void)womanClick
{
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
        
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
}

-(void)manClick
{
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
        
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
        
    }
}

-(void)saveClick
{
    [self hiddenKeyboard];
    if (![self validateInputValue]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:groupNameTextField.text forKey:@"groupName"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:groupManagerNameTextField.text forKey:@"groupManagerName"];
    [postDict setValue:groupManagerCertificateTextField.text forKey:@"identity"];
    [postDict setValue:groupManagerAccountTextField.text forKey:@"groupManager"];
    [postDict setValue:sexFlag forKey:@"gender"];
    if (mobileBtn.selected) {
        [postDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
    }
    
    if([self getTimestamp:_communityId timeKey:@"group_timestamp"] != nil)
    {
        [postDict setValue:[self getTimestamp:_communityId timeKey:@"group_timestamp"] forKey:@"timestamp"];
    }
    else
    {
        [postDict setValue:@"" forKey:@"timestamp"];
    }
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(ADDGROUP) params:postDict block:^(id downLoadData, id error)
    {
        [self stopWaiting];
        if([downLoadData objectForKey:@"cache"] != nil)
        {
            if([@"1" isEqualToString:[NSString stringWithFormat:@"%@",[[downLoadData objectForKey:@"cache"] objectForKey:@"status"]]]==YES)
            {
                NSMutableDictionary *data = [downLoadData objectForKey:@"data"];
                GuestModel * gModel = [[GuestModel alloc]init];
                gModel.guestName = [data objectForKey:@"groupManagerName"];
                gModel.sex = [NSString stringWithFormat:@"%@",[data objectForKey:@"gender"]];
                gModel.email = [data objectForKey:@"groupManagerEmail"];
                gModel.telePhone = [data objectForKey:@"groupManagerMobile"];
                gModel.isGroupManager = @"1";
                gModel.certificaties = [data objectForKey:@"identity"];
                gModel.headUrl = [data objectForKey:@"groupManagerHeadurl"];
                gModel.guestid = [data objectForKey:@"groupManagerId"];
                if (mobileBtn.selected) {
                    gModel.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                }
                [QRSingleton sharedQRSingleton].groupEndTime = [data objectForKey:@"endtime"];
                [QRSingleton sharedQRSingleton].groupBeginTime = [data objectForKey:@"starttime"];
                groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                gifv.groupId = [data objectForKey:@"groupId"];
                gifv.groupName = [data objectForKey:@"groupName"];
                gifv.model = gModel;
                gifv.communityId = _communityId;
                gifv.isGroup = YES;
                [QRSingleton sharedQRSingleton].groupPush = @"0";
                
                OperationDataBaseModel * opData = [[OperationDataBaseModel alloc]init];
                NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                [dic setObject:gifv.groupId forKey:@"groupId"];
                [dic setObject:gifv.groupName forKey:@"groupName"];
                [dic setObject:gModel.telePhone forKey:@"groupManagerMobile"];
                [dic setObject:gModel.email forKey:@"groupManagerEmail"];
                [dic setObject:gModel.guestName forKey:@"groupManagerName"];
                [dic setObject:[QRSingleton sharedQRSingleton].groupBeginTime forKey:@"starttime"];
                [dic setObject:[QRSingleton sharedQRSingleton].groupEndTime forKey:@"endtime"];
                [dic setObject:_communityId forKey:@"communityId"];
                [dic setObject:@"" forKey:@"groupClient_timestamp"];
                
                [opData insertDataTable:@"groupInfo" withDict:dic];
                [self.navigationController pushViewController:gifv animated:YES];
            }
            else
            {
                [self updateTimestamp:_communityId timeKey:@"group_timestamp" timeKeyValue:[[downLoadData objectForKey:@"cache"] objectForKey:@"timestamp"]];
                if ([downLoadData objectForKey:@"data"] != nil)
                {
                    NSMutableDictionary *data = [downLoadData objectForKey:@"data"];
                    GuestModel * gModel = [[GuestModel alloc]init];
                    gModel.guestName = [data objectForKey:@"groupManagerName"];
                    gModel.sex = [NSString stringWithFormat:@"%@",[data objectForKey:@"gender"]];
                    gModel.email = [data objectForKey:@"groupManagerEmail"];
                    gModel.telePhone = [data objectForKey:@"groupManagerMobile"];
                    gModel.isGroupManager = @"1";
                    gModel.guestid = [data objectForKey:@"groupManagerId"];
                    gModel.certificaties = [data objectForKey:@"identity"];
                    gModel.headUrl = [data objectForKey:@"groupManagerHeadurl"];
                    if (mobileBtn.selected) {
                        gModel.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                    }
                    [QRSingleton sharedQRSingleton].groupEndTime = [data objectForKey:@"endtime"];
                    [QRSingleton sharedQRSingleton].groupBeginTime = [data objectForKey:@"starttime"];
                    groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                    gifv.groupId = [data objectForKey:@"groupId"];
                    gifv.groupName = [data objectForKey:@"groupName"];
                    gifv.model = gModel;
                    gifv.communityId = _communityId;
                    gifv.isGroup = YES;
                    [QRSingleton sharedQRSingleton].groupPush = @"0";
                    
                    OperationDataBaseModel * opData = [[OperationDataBaseModel alloc]init];
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
                    [dic setObject:gifv.groupId forKey:@"groupId"];
                    [dic setObject:gifv.groupName forKey:@"groupName"];
                    [dic setObject:gModel.telePhone forKey:@"groupManagerMobile"];
                    [dic setObject:gModel.email forKey:@"groupManagerEmail"];
                    [dic setObject:gModel.guestName forKey:@"groupManagerName"];
                    if (mobileBtn.selected) {
                       [dic setObject:gModel.areaCode forKey:@"areaCode"];
                    }
                    
                    [dic setObject:[QRSingleton sharedQRSingleton].groupBeginTime forKey:@"starttime"];
                    [dic setObject:[QRSingleton sharedQRSingleton].groupEndTime forKey:@"endtime"];
                    [dic setObject:_communityId forKey:@"communityId"];
                    [dic setObject:@"" forKey:@"groupClient_timestamp"];
                    
                    [opData insertDataTable:@"groupInfo" withDict:dic];
                    [self.navigationController pushViewController:gifv animated:YES];
                }
            }
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
        }
    }];
}


#pragma mark -- 提交数据之前的判断
-(BOOL)validateInputValue
{
    if (groupNameTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorGroupName", nil)];
        return NO;
    }
    if (groupManagerAccountTextField.text.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorAccount", nil)];
        return NO;
    }
    if (mobileBtn.selected) {
        if (![validateInputValue isPureNumandCharacters:groupManagerAccountTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return NO;
        }
    }
    else if (emailBtn.selected)
    {
        if (![validateInputValue validateEmail:groupManagerAccountTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil) superView:self.view];
            return NO;
        }
    }

    if (groupManagerNameTextField.text.length == 0 ||groupManagerAccountTextField.text.length > 40) {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil)];
        return NO;
    }
        return YES;
}

-(void)BackClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)hiddenKeyboard
{
    [groupNameTextField resignFirstResponder];
    [groupManagerAccountTextField resignFirstResponder];
    [groupManagerCertificateTextField resignFirstResponder];
    [groupManagerNameTextField resignFirstResponder];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
